<?php
namespace app\api\controller\v1\model\pet;

use think\Model;
use think\Db;


class Fosterage extends model
{
//显示寄样表信息
    public function getFosterageForm($fosterage_id){
        $result = Db::table('ym_fosterage')->alias('a')->join('ym_fosterage_img b','a.fosterage_id = b.fosterage_id')->join('user_info c','a.user_id = c.user_id')->field(['c.user_name','c.user_icon','a.user_id','a.fosterage_id','a.fosterage_name','a.fosterage_phone','a.fosterage_reason','a.fosterage_matters_need_attention','fosterage_start_time','fosterage_end_time','a.fosterage_status','b.fosterage_img'])->where('a.fosterage_id',$fosterage_id)->select();

        return $result;
    }


    //同意或者不同意寄养
    public function fosterage_agree_or_refuse($fosterage_agree,$reason,$user_id,$fosterage_id){
        $time = date('Y-m-d H:i:s');
        $be_id = Db::query("select user_id from ym_fosterage where fosterage_id = '$fosterage_id'");
        $be_user_id = $be_id[0]['user_id'];
        $user_name = Db::query("select user_name from user_info  where user_id = '$user_id'");
        $name = $user_name[0]['user_name'];
        if ($fosterage_agree == 0) {
            //拒绝收养
            $refuse_data = ['fosterage_id'=>$fosterage_id,'user_id'=>$user_id,'fosterage_agree'=>$fosterage_agree,'fosterage_refuse_reason'=>$reason];
            Db::table('ym_fosterage_agree_or_refuse')->insert($refuse_data);
            Db::query("update ym_fosterage set fosterage_status = 0 where fosterage_id = '$fosterage_id'");
            //message
            $message_data = ['be_user_id'=>$be_user_id,'user_id'=>$user_id,'message_type'=>7,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $fosterage_data = ['m_id'=>$m_id,'refuse_reason'=>$reason,'fosterage_id'=>$fosterage_id];
            $m_fosterage_id = Db::table('ym_message_fosterage_agree_or_not')->insertGetId($fosterage_data);

            $be_user_id = strval($be_user_id);
            $this->push($be_user_id,$name.'拒绝收养的你宠物,原因:'.$reason);
            return $this->returnmsg(200,"拒绝收养",$refuse_data);
        }else{
            $agree_data = ['fosterage_id'=>$fosterage_id,'user_id'=>$user_id,'fosterage_agree'=>$fosterage_agree];
            Db::table('ym_fosterage_agree_or_refuse')->insert($agree_data);

            Db::query("update ym_fosterage set fosterage_status = 1 where fosterage_id = '$fosterage_id'");

            //message
            $message_data = ['be_user_id'=>$be_user_id,'user_id'=>$user_id,'message_type'=>7,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $fosterage_data = ['m_id'=>$m_id,'fosterage_id'=>$fosterage_id];
            $m_fosterage_id = Db::table('ym_message_fosterage_agree_or_not')->insertGetId($fosterage_data);

            $be_user_id = strval($be_user_id);
            $this->push($be_user_id,$name.'同意收养的你宠物');
            return $this->returnmsg(200,"同意收养",$agree_data);
        }
    }




























}